﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xfs;

namespace Xfs
{
    [XfsObjectSystem]
    public class XfsGridMapComponentAwakeSystem : XfsAwakeSystem<XfsGridMapComponent>
    {
        public override void Awake(XfsGridMapComponent self)
        {
            XfsGridMap? gridMap = XfsComponentFactory.CreateWithParent<XfsGridMap>(self);
            if (gridMap == null) return;
            gridMap.Id = self.CurGridMapId;
            gridMap.Name = "测试地图";
            gridMap.WithSize = 400;
            gridMap.HighSize = 400;
            gridMap.with = 40;
            gridMap.high = 40;
            gridMap.withMul = 10;
            gridMap.highMul = 10;

            gridMap.Grids = new XfsGrid[gridMap.with, gridMap.high];

            self.GridMaps.Add(gridMap.Id, gridMap);

            for (int i = 0; i < gridMap.with; i++)
            {
                for (int j = 0; j < gridMap.high; j++)
                {
                    XfsGrid? grid = XfsComponentFactory.Create<XfsGrid>();

                    if (grid == null) return;

                    grid.x = i;
                    grid.z = j;

                    gridMap.Grids[i, j] = grid;

                    gridMap.GridList.Add(grid);

                    if ((i > 10) && (i <= (gridMap.with - 10)) && (j == 20))
                    {
                        grid.ToObstacle();

                        gridMap.Obstacles.Add(grid);
                    }
                }
            }
            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 58. gridMap-id/name: " + self.GridMaps.Values.ToList()[0].Id + " / " + self.GridMaps.Values.ToList()[0].Name + " / " + self.GridMaps.Values.ToList()[0].Count);
            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 59. gridMap-obs/cout: " + self.GridMaps.Values.ToList()[0].Obstacles.Count + " / " + self.GridMaps.Values.ToList()[0].GridList.Count);
        }
    }

    [XfsObjectSystem]
    public class XfsGridMapComponentStartSystem : XfsStartSystem<XfsGridMapComponent>
    {
        public override void Start(XfsGridMapComponent self)
        {

        }
    }

    [XfsObjectSystem]
    public class XfsGridMapComponentUpdateSystem : XfsUpdateSystem<XfsGridMapComponent>
    {
        public override void Update(XfsGridMapComponent self)
        {

        }
    }

    public static class XfsGridMapComponentHelper
    {
        #region ToGrids ToVector3
        public static XfsGrid? ToGrid(this XfsGridMapComponent self, Vector3 v3)
        {
            XfsGridMap? gridMap = self.GetGridMap();

            int with = (int)Math.Floor((double)(v3.x / gridMap.withMul));
            int high = (int)Math.Floor((double)(v3.z / gridMap.highMul));

            return gridMap.Grids?[with, high];
        }

        public static Vector3 ToVector3(this XfsGridMapComponent self, XfsGrid grid)
        {
            XfsGridMap? gridMap = self.GetGridMap();

            float withsize = (float)(grid.x * gridMap.withMul);
            float highsize = (float)(grid.z * gridMap.highMul);

            return new Vector3(withsize, 0, highsize);
        }

        public static List<Vector3> ToVector3List(this XfsGridMapComponent self, ArrayList grids)
        {
            List<Vector3> v3es = new List<Vector3>();

            if (grids == null || grids.Count == 0) return v3es;

            foreach (XfsGrid tem in grids)
            {
                Vector3 vec = self.ToVector3(tem);
                v3es.Add(vec);
            }
            return v3es;
        }

        public static XfsGridMap? GetGridMap(this XfsGridMapComponent self)
        {
            XfsGridMap? gridMap = null;

            if (self.GridMaps.TryGetValue(self.CurGridMapId, out gridMap))
            {
                return gridMap;
            }

            return gridMap;
        }

        public static XfsGridMap? GetGridMap(this XfsGridMapComponent self, long id)
        {
            XfsGridMap? gridMap = null;
            if (self.GridMaps.TryGetValue(id, out gridMap))
            {
                return gridMap;
            }
            return gridMap;
        }

        #endregion


    }
}
